
/*

Design Pattern Automation Toolkit.
Application to create applications with emphasis on Design patterns.
And support for round trip engineering.
Copyright (C) 2004 Vineeth Neelakant. nvineeth@gmail.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/
using System;using BusinessLayer;using InfoClasses;using dpatoolkit.UILayer;
using System.Data;
using DesignPatternDefinitions;
using System.Windows.Forms;
using System.Collections;

namespace BusinessLayer
{
	/// <summary>
	/// concrete class which implements IMEthod
	/// </summary>
	public class DPMethod : IMethod
	{
		private DataSet dataset;
		private DataTable methodTable;
		private DataRow methodRow;
		private string compName,cName;
		private int mid;

		public DPMethod( string comp , string cname,int methid , DataRow mrow)
		{
			this.dataset=mrow.Table.DataSet;
			this.compName=comp;
			this.cName=cname;
			this.mid=methid;
			this.methodTable=this.dataset.Tables[SchemaNames.Methods];
			this.methodRow=mrow;
		}

		#region IMethod Members
		public string GetMetaInfo(string language)
		{
			MetaInfoEditor mie = new MetaInfoEditor((string)this.methodRow [SchemaNames.MetaInfo]);
			return mie.GetMetaInfo(language);
		}
		public void SetMetaInfo(string minfo)
		{
			//Not required.....protection against malicious plugins.
		}

		public DesignPatternDefinitions.MethodType MethodType
		{
			get
			{
				string mtype =(string) this.methodRow[SchemaNames.MethodType] ;
				if(mtype==DesignPatternDefinitions.MethodType.Abstract.ToString().ToLower() )
					return MethodType.Abstract;
				if(mtype==DesignPatternDefinitions.MethodType.Concrete.ToString().ToLower())
					return MethodType.Concrete;
				return MethodType.Imported;

			}
			set
			{
				this.methodRow[SchemaNames.MethodType] = value.ToString().ToLower();
			}
		}

		public string UMLRepresentation
		{
			get
			{
				//go thru all the parameters and construct !
				DataRow [] paramRows = this.dataset.Tables[SchemaNames.Parameters].
					Select( SchemaNames.MethodID+"='"+mid+"'" , SchemaNames.ParamOrder);
				string uml="";
				switch ( (string)this.methodRow[SchemaNames.Visibility])
				{
					case "public":
						uml+="+";
						break;
					case "private":
						uml+="-";
						break;
					default:
						uml+="#";
						break;

				}
				uml+=(string)this.methodRow[SchemaNames.MethodName] +
					"(";
				foreach (DataRow row in  paramRows)
				{
					uml+=new DPParameter(row).GetUMLRepresentation();
					uml+=",";
				}
				if(uml.EndsWith(","))
					uml=uml.Remove(uml.Length-1,1);
				uml+="):" + (string)this.methodRow[SchemaNames.MethodReturnType];

				return uml;
				
			}

			
		}

		public object MethodUID
		{
			get{ return (int)this.methodRow[SchemaNames.MethodID];}
		}

		public string MethodName 
		{
			get { return (string)this.methodRow[SchemaNames.MethodName];}
			set {}

		}

		public string MethodVisibility 
		{
			get { return (string)this.methodRow[SchemaNames.Visibility];}
			set {}

		}

		public string MethodReturnType 
		{
			get 
			{
				return (string)this.methodRow[SchemaNames.MethodReturnType];
			}
			set {}

		}

		public ArrayList Parameters 
		{
			get 
			{
				DataRow [] paramRows = this.dataset.Tables[SchemaNames.Parameters].
					Select( SchemaNames.MethodID+"='"+mid+"'" , SchemaNames.ParamOrder);
				ArrayList Params = new ArrayList();
				foreach(DataRow d in paramRows)
					Params.Add(new DPParameter (d));
				return Params;
			}
			set {}
		}


		public string MethodSummary 
		{
			get 
			{
				return (string)this.methodRow[SchemaNames.Summary];
			}

			set 
			{
				this.methodRow[SchemaNames.Summary]=(string)value;
				
			}
		}

		public Hashtable GetAllMetaInfo()
		{
			MetaInfoEditor mieditor = new MetaInfoEditor((string) this.methodRow[SchemaNames.MetaInfo] );
			return mieditor.GetAllMetaInfo();
		}

		#endregion
	}
}
